package com.lanou.lenanapp.adapter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.activity.ReviewActivity;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.CollectEntity;
import com.lanou.lenanapp.entity.greendao.JokeEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 段子列表的适配器
 */
public class JokeFragmentAdapter extends RecyclerView.Adapter<JokeFragmentAdapter.JokeViewholder> {

    private List<JokeEntity> items;//段子的实体类
    private List<CollectEntity> collectEntities;//收藏的实体类
    private Context context;
    private DatabaseHelper helper;

    public JokeFragmentAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<JokeEntity> datas) {
        this.items = datas;
        notifyDataSetChanged();//数据改变通知页面刷新
    }

    public void loadData(List<JokeEntity> datas) {
        this.items.addAll(datas);
        notifyDataSetChanged();
    }


    /**
     * 组件缓存内部类
     **/
    class JokeViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView updata_time, wbody, like, review;
        private ImageView favorited, like_one_joke, like_image_joke;
        private int position;

        public JokeViewholder(View itemView) {
            super(itemView);
            helper = DatabaseHelper.getHelper();//初始化helper
            updata_time = (TextView) itemView.findViewById(R.id.updata_time);//发布时间
            wbody = (TextView) itemView.findViewById(R.id.wbody);//段子内容
            like = (TextView) itemView.findViewById(R.id.like);//赞的数量
            review = (TextView) itemView.findViewById(R.id.review);//评论按钮
            favorited = (ImageView) itemView.findViewById(R.id.favorited);//收藏
            like_one_joke = (ImageView) itemView.findViewById(R.id.like_one_joke);//+1动画
            like_image_joke = (ImageView) itemView.findViewById(R.id.like_image_joke);//赞的图片
            like_image_joke.setOnClickListener(this);
            favorited.setOnClickListener(this);
            review.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.review://评论按钮  将wid属性传到评论页面
                    if (items != null && items.size() > 0) {
                        JokeEntity joke = items.get(position);
                        Intent intent = new Intent(context, ReviewActivity.class);
                        intent.putExtra("id", joke.getWid());
                        context.startActivity(intent);
                    }
                    break;
                case R.id.favorited://收藏按钮
                    saveJoke();
                    break;
                case R.id.like_image_joke://赞
                    isLike();
                    break;
            }
        }


        /**
         * 点赞
         * **/
        private void isLike() {
            JokeEntity joke = items.get(position);
            int t = joke.getIsChange();//获得实体类里自己定义的ischane属性
            if (t == 0) {
                like_image_joke.setImageResource(R.drawable.icon_liked);//点击换图片
                like_one_joke.setVisibility(View.VISIBLE);//将动画控件显示
                startAnim(like_one_joke);//属性动画播放

                t = t + 1;
                joke.setIsChange(t);//将加1后的t设置给ischane 下次获得是ischane得值就是1

                //显示的数字加一
                double d = Double.parseDouble(joke.getLikes());
                int l = (int) d;
                l++;
                like.setText(l + "");
                joke.setLikes(l + "");
            } else if (t == 1) {
                Toast.makeText(context, "您已经赞过了", Toast.LENGTH_SHORT).show();
            }
        }

        /**
         * 收藏
         * **/
        private void saveJoke() {
            //取到当前点击行布局得内容
            JokeEntity jokeEntity = items.get(position);
            int t = jokeEntity.getIsChangefavourate();
            if (t == 0) {
                String com = jokeEntity.getComments();
                String li = jokeEntity.getLikes();
                String wid = jokeEntity.getWid();
                String time = jokeEntity.getUpdate_time();
                String wb = jokeEntity.getWbody();
                String sen = jokeEntity.getW_sensitive();

                CollectEntity collectEntity = new CollectEntity();
                //设置到collect实体类里
                collectEntity.setComments(com);
                collectEntity.setLikes(li);
                collectEntity.setWid(wid);
                collectEntity.setUpdate_time(time);
                collectEntity.setWbody(wb);
                collectEntity.setW_sensitive(sen);
                //添加到数据库
                DatabaseHelper.getHelper().saveCollectList(collectEntity);
                favorited.setImageResource(R.drawable.icon_favorited);
                Toast.makeText(context, "收藏成功..快去看看吧", Toast.LENGTH_SHORT).show();

                t = t + 1;
                jokeEntity.setIsChangefavourate(t);
            }else if (t == 1){
                String wid = jokeEntity.getWid();
                helper.deleteByIdCollect(wid);
                favorited.setImageResource(R.drawable.icon_favorite);
                Toast.makeText(context, "已取消收藏..", Toast.LENGTH_SHORT).show();
            }



        }

        /**
         * 属性动画方法
         **/
        private void startAnim(final View view) {
            ObjectAnimator animator = new ObjectAnimator().ofFloat(view, "translationY", 0f, -100f);
            animator.setDuration(2000);//设置播放时间 和延时播放时间
            //给属性动画加监听
            animator.addListener(new AnimatorListenerAdapter() {
                //复写动画播放结束的方法
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    //播放完成后隐藏控件
                    view.setVisibility(View.GONE);
                }
            });
            animator.start();
        }
    }


    @Override
    public JokeViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        //获得布局加载器
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        //加载行布局
        View v = inflater.inflate(R.layout.activity_fragment_joke_gut, null);
        return new JokeViewholder(v);
    }


    @Override
    public void onBindViewHolder(JokeViewholder holder, int position) {

        if (items != null && items.size() > 0) {
            holder.position = position;

            JokeEntity joke = items.get(position);
            //时间转换 并赋值给time
            String time = getNormalTime(joke.getUpdate_time());
            holder.updata_time.setText(time);//设置时间

            holder.wbody.setText(joke.getWbody());
            double t = Double.parseDouble(joke.getLikes());
            int m = (int) t;
            holder.like.setText(m + "");
            holder.review.setText(joke.getComments());

            //判断ischange的值 设置赞的图片样式 防止翻页后乱套
            int isChange = joke.getIsChange();
            if (isChange == 0) {
                holder.like_image_joke.setImageResource(R.drawable.icon_like);
            } else if (isChange == 1) {
                holder.like_image_joke.setImageResource(R.drawable.icon_liked);
            }
            int isChangefavourate = joke.getIsChangefavourate();
            if (isChangefavourate == 0){
                holder.favorited.setImageResource(R.drawable.icon_favorite);
            }else if (isChangefavourate == 1){
                holder.favorited.setImageResource(R.drawable.icon_favorited);
            }

        }
    }

    //时间转换
    public String getNormalTime(String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time;
    }


    @Override
    public int getItemCount() {
        return items != null && items.size() > 0 ? items.size() : 0;
    }


}
